﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;

namespace ElingOA6_DSJCJ.Controllers
{
    public class WebMapController : Controller
    {
        BLL.Config configBLL = new BLL.Config();
        BLL.ConfigEntry configEntryBLL = new BLL.ConfigEntry();
        BLL.DocCls docClsBLL = new BLL.DocCls();

        /// <summary>
        /// 网站地图
        /// </summary>
        /// <returns></returns>
        [OutputCache(Duration = 0, VaryByParam = "none")]
        public ActionResult Index()
        {
            List<Models.WebMapGridModels> webMapList = new List<Models.WebMapGridModels>();
            Models.WebMapGridModels webMapModel = new Models.WebMapGridModels();
            List<Models.WebMapGridModels> tmpWebMapList = new List<Models.WebMapGridModels>();

            int id = 1;
            //首页栏目
            webMapModel = new Models.WebMapGridModels();
            webMapModel.FPreId = null;
            webMapModel.FId = id.ToString("00");
            webMapModel.FName = "首页栏目";
            webMapModel.FUrl = null;
            webMapList.Add(webMapModel);
            //子栏目
            int beginId = 1;
            tmpWebMapList = GetWebMapListByConfig("01", Common.GlobalConstant.ENTITYNO, webMapModel.FId, ref beginId);//首页导航栏目
            webMapList.AddRange(tmpWebMapList);
            tmpWebMapList = GetWebMapListByConfig("04", Common.GlobalConstant.ENTITYNO, webMapModel.FId,ref beginId);//首页显示栏目
            webMapList.AddRange(tmpWebMapList);
            id++;

            //专题专栏
            webMapModel = new Models.WebMapGridModels();
            webMapModel.FPreId = null;
            webMapModel.FId = id.ToString("00");
            webMapModel.FName = "专题专栏";
            webMapModel.FUrl = null;
            webMapList.Add(webMapModel);
            //子栏目
            beginId = 1;
            string preDocClsCd = Common.GenerateUtil.GetConfigValue("doc_ztzl_cls");
            tmpWebMapList = GetWebMapListByFDocClsCd(preDocClsCd, webMapModel.FId, ref beginId);//首页导航栏目
            webMapList.AddRange(tmpWebMapList);
            id++;

            //业务模块
            webMapModel = new Models.WebMapGridModels();
            webMapModel.FPreId = null;
            webMapModel.FId = id.ToString("00");
            webMapModel.FName = "业务模块";
            webMapModel.FUrl = null;
            webMapList.Add(webMapModel);
            //子栏目
            beginId = 1;
            tmpWebMapList = GetWebMapListByConfig("02", Common.GlobalConstant.ENTITYNO, webMapModel.FId, ref beginId);//应用系统连接
            webMapList.AddRange(tmpWebMapList);
            id++;

            //机关科室
            webMapModel = new Models.WebMapGridModels();
            webMapModel.FPreId = null;
            webMapModel.FId = id.ToString("00");
            webMapModel.FName = "机关科室";
            webMapModel.FUrl = null;
            webMapList.Add(webMapModel);
            //子栏目
            beginId = 1;
            tmpWebMapList = GetWebMapListByConfig("06", Common.GlobalConstant.ENTITYNO, webMapModel.FId, ref beginId);//应用系统连接
            webMapList.AddRange(tmpWebMapList);
            id++;

            //地税分局
            webMapModel = new Models.WebMapGridModels();
            webMapModel.FPreId = null;
            webMapModel.FId = id.ToString("00");
            webMapModel.FName = "地税分局";
            webMapModel.FUrl = null;
            webMapList.Add(webMapModel);
            //子栏目
            beginId = 1;
            tmpWebMapList = GetWebMapListByConfig("07", Common.GlobalConstant.ENTITYNO, webMapModel.FId, ref beginId);//应用系统连接
            webMapList.AddRange(tmpWebMapList);
            id++;

            //直属单位
            webMapModel = new Models.WebMapGridModels();
            webMapModel.FPreId = null;
            webMapModel.FId = id.ToString("00");
            webMapModel.FName = "直属单位";
            webMapModel.FUrl = null;
            webMapList.Add(webMapModel);
            //子栏目
            beginId = 1;
            tmpWebMapList = GetWebMapListByConfig("08", Common.GlobalConstant.ENTITYNO, webMapModel.FId, ref beginId);//应用系统连接
            webMapList.AddRange(tmpWebMapList);
            id++;


            return View(webMapList);
        }

        /// <summary>
        /// 根据配置文件 获取网站地图
        /// </summary>
        /// <param name="FConfigClass"></param>
        /// <param name="FEntityNo"></param>
        /// <param name="FPreId"></param>
        /// <returns></returns>
        private List<Models.WebMapGridModels> GetWebMapListByConfig(string FConfigClass, string FEntityNo, string FPreId,ref int beginId)
        {
            List<Models.WebMapGridModels> webMapList = new List<Models.WebMapGridModels>();
            Models.WebMapGridModels webMapModel = new Models.WebMapGridModels();
            Models.WebMapGridModels childWebMapModel = new Models.WebMapGridModels();

            List<Model.Config> configListALL= configBLL.GetListAllIsUse(FEntityNo);
            List<Model.ConfigEntry> configEntryList = configEntryBLL.GetModelListWithShowText(FEntityNo, FConfigClass);
            List<Model.Config> configList = (from c in configListALL
                                              where c.FConfigClass==FConfigClass
                                              orderby c.FSeq
                                              select c).ToList();

            //如果是 首页导航栏目 去除 廉政之窗
            if (FConfigClass == "01")
            {
                configList = (from c in configList
                              where c.FKeyName != "doc_lzzc"
                              select c).ToList();
            }
            //如果是 首页显示栏目 去除 科室动态、专题专栏、廉政之窗
            if (FConfigClass == "04")
            {
                configList = (from c in configList
                              where c.FKeyName != "doc_ksdt" && c.FKeyName != "doc_ztzl" && c.FKeyName != "doc_lzzc"
                              select c).ToList();
            }

            int id = beginId;
            foreach (Model.Config configModel in configList)
            {
                //先插入配置主表
                webMapModel = new Models.WebMapGridModels();
                webMapModel.FPreId = FPreId;
                webMapModel.FId = webMapModel.FPreId + id.ToString("00");
                webMapModel.FName = configModel.FShowText;
                #region URL和子栏目
                if (configModel.FIsWithDocCls == "Y")
                {
                    DataTable configDocClsDT = configEntryBLL.GetConfigDocCls(FEntityNo, configModel.FKeyName, configModel.FConfigClass);
                    var tmpDocCls = configDocClsDT.Select("ClsLevel=0").ToList();
                    if (configDocClsDT.Rows.Count == 0)//如果无栏目
                    {
                        webMapModel.FUrl = null;
                        tmpDocCls = null;
                    }
                    else
                    {
                        //如果栏目只有一个 或者 栏目有两个但本级栏目只有一个
                        if (configDocClsDT.Rows.Count == 1 || (tmpDocCls.Count() == 1 && configDocClsDT.Rows.Count == 2))
                        {
                            webMapModel.FUrl = "/Index/DocumentIndex?keyName=" + configModel.FKeyName + "&keyClass=" + configModel.FConfigClass;
                            tmpDocCls = null;
                        }
                        else
                        {
                            if (tmpDocCls.Count() == 1)//如果本级栏目只有一个
                            {
                                //去除0层的节点
                                configDocClsDT.Rows.Remove(tmpDocCls[0]);
                                tmpDocCls = configDocClsDT.Select("ClsLevel=1").ToList();
                            }
                            else//如果本级栏目有多个
                            {
                                tmpDocCls = configDocClsDT.Select("ClsLevel<0 ").ToList();
                                if (tmpDocCls.Count() == 0)
                                {
                                    tmpDocCls = configDocClsDT.Select("ClsLevel=0 ").ToList();
                                }
                                else
                                {
                                    tmpDocCls = configDocClsDT.Select("ClsLevel<0 OR FPreDocClsCd IS NULL ", "FSeq,FDocClsCd").ToList();

                                }
                            }
                        }
                    }

                    //再插入配置明细表
                    #region 子栏目
                    if (tmpDocCls != null)
                    {
                        if (tmpDocCls.Count() > 0)
                        {
                            int childId = 1;
                            foreach (DataRow dr in tmpDocCls)
                            {
                                childWebMapModel = new Models.WebMapGridModels();
                                childWebMapModel.FPreId = FPreId + id.ToString("00");
                                childWebMapModel.FId = childWebMapModel.FPreId + childId.ToString("00");
                                childWebMapModel.FName = dr["FDocClsName"].ToString();
                                childWebMapModel.FUrl = "/Index/DocumentIndex?keyName=" + configModel.FKeyName + "&keyClass=" + configModel.FConfigClass + "&indexClsCd=" + dr["FDocClsCd"].ToString() + "&docClsCd=" + dr["FDocClsCd"].ToString();
                                webMapList.Add(childWebMapModel);
                                childId++;
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    webMapModel.FUrl = configModel.FKeyValue;
                }
                #endregion
                webMapList.Add(webMapModel);
                id++;
            }

            beginId = id;
            return webMapList;
        }

        /// <summary>
        /// 根据栏目编号获取网站地图
        /// </summary>
        /// <param name="docClsCd"></param>
        /// <returns></returns>
        private List<Models.WebMapGridModels> GetWebMapListByFDocClsCd(string docClsCd, string FPreId, ref int beginId)
        {
            List<Models.WebMapGridModels> webMapList = new List<Models.WebMapGridModels>();
            Models.WebMapGridModels webMapModel = new Models.WebMapGridModels();

            DataSet ds= docClsBLL.GetSubDocCls(docClsCd,Common.GlobalConstant.ENTITYNO);

            int id = beginId;
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    webMapModel = new Models.WebMapGridModels();
                    webMapModel.FPreId = FPreId;
                    webMapModel.FId = webMapModel.FPreId + id.ToString("00");
                    webMapModel.FName = dr["FDocClsName"].ToString();
                    webMapModel.FUrl = "/Column/ZtzlColumn?clsCd=" + dr["FDocClsCd"].ToString();
                    webMapList.Add(webMapModel);
                    id++;
                }
            }

            beginId = id;
            return webMapList;
        }


    }
}
